package com.focusai.efairy.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.focusai.efairy.R;
import com.focusai.efairy.model.location.MapType;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by luo_guangwen on 2017/11/20.
 */

public class MapLocationUtils {
    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context
     * @param packageName：应用包名
     * @return
     */
    public static boolean isAvilible(Context context, String packageName) {
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

    /**
     * 百度地图
     */
    public static boolean hasBaiDuMap(Context context) {
        return isAvilible(context, "com.baidu.BaiduMap");
    }

    /**
     * 高德地图
     */
    public static boolean hasGaoDeMap(Context context) {
        return isAvilible(context, "com.autonavi.minimap");
    }

    public static boolean jumpToMap(Context context, MapType mapType) {
        if (mapType.getMapType() == MapType.GAODE_MAP) { //高德
            return jumpToGaode(context, mapType);
        } else if (mapType.getMapType() == MapType.BAIDU_MAP) {
            return jumpToBaidu(context, mapType);
        }
        return false;
    }

    public static boolean jumpToBaidu(Context context, MapType mapType) {
        try {
//                          intent = Intent.getIntent("intent://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving®ion=西安&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
            Intent intent = Intent.getIntent("intent://map/direction?" +
                    //"origin=latlng:"+"34.264642646862,108.95108518068&" +   //起点  此处不传值默认选择当前位置
                    "destination=latlng:" + mapType.getEndLatitude() + "," + mapType.getEndLongitude() + "|name:" + mapType.getEndName() +        //终点
                    "&mode=driving&" +          //导航路线方式
                    "region=北京" +           //
                    "&src=" + context.getResources().getString(R.string.app_name) + "#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
            context.startActivity(intent); //启动调用
            return true;
        } catch (URISyntaxException e) {
            Log.e("intent", e.getMessage());
            return false;
        }
    }

    public static boolean jumpToGaode(Context context, MapType mapType) {
//        try {
////            Intent intent = Intent.getIntent("androidamap://navi?sourceApplication=慧医&poiname=我的目的地&lat=" + lat + "&lon=" + lng + "&dev=0");
////            context.startActivity(intent);
//
//            StringBuffer stringBuffer = new StringBuffer("androidamap://route?sourceApplication=").append("amap");
//
//            stringBuffer.append("&dlat=").append(mapType.getEndLatitude())
//                    .append("&dlon=").append(mapType.getEndLongitude())
//                    .append("&dname=").append(mapType.getEndName())
//                    .append("&dev=").append(0)
//                    .append("&t=").append(mapType.getMode());
//
//            Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(stringBuffer.toString()));
//            intent.setPackage("com.autonavi.minimap");
//            context.startActivity(intent);
//
//            return true;
//        } catch (URISyntaxException e) {
//            e.printStackTrace();
//            return false;
//        }

        StringBuffer stringBuffer = new StringBuffer("androidamap://route?sourceApplication=").append("amap");

        stringBuffer.append("&dlat=").append(mapType.getEndLatitude())
                .append("&dlon=").append(mapType.getEndLongitude())
                .append("&dname=").append(mapType.getEndName())
                .append("&dev=").append(0)
                .append("&t=").append(mapType.getMode());

        Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(stringBuffer.toString()));
        intent.setPackage("com.autonavi.minimap");
        context.startActivity(intent);

        return true;
    }
}
